package cn.kow.kmall.controller.system;

import cn.kow.kmall.common.enums.KmallResultEnum;
import cn.kow.kmall.controller.BaseController;
import cn.kow.kmall.core.exception.ServiceException;
import cn.kow.kmall.core.response.KmallResult;
import cn.kow.kmall.system.entity.SysMenu;
import cn.kow.kmall.system.entity.SysRole;
import cn.kow.kmall.admin.pojo.dto.RoleEditDTO;
import cn.kow.kmall.admin.pojo.dto.RoleQueryDTO;
import cn.kow.kmall.admin.pojo.vo.RoleVO;
import cn.kow.kmall.system.service.SysMenuService;
import cn.kow.kmall.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lijun
 * @Description:
 * @Date: Created in 2019-10-29 17:04
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysMenuService menuService;

    /**
     * 新增角色
     * @param roleDTO  角色
     * @return 结果
     */
    @PostMapping(value = "/add")
    public KmallResult add(@Validated @RequestBody RoleEditDTO roleDTO) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleCode(roleDTO.getRoleCode());
        sysRole.setRoleName(roleDTO.getRoleName());
        sysRole.setStatus(roleDTO.getStatus());
        sysRole.setSort(roleDTO.getSort());
        sysRole.setRemark(roleDTO.getRemark());
        List<Long> menuIds = roleDTO.getMenuIds();
        if (!CollectionUtils.isEmpty(menuIds)) {
            List<SysMenu> menus = menuService.listByIds(menuIds);
            sysRole.setMenus(menus);
        }
        sysRole.setUpdateBy(currentUser().getUsername());
        sysRole.setCreateBy(currentUser().getUsername());
        roleService.save(sysRole);
        return KmallResult.success();
    }

    /**
     * 删除角色
     * @param id    角色ID
     * @return 结果
     */
    @DeleteMapping("/{id}")
    public KmallResult remove(@PathVariable Long id) {
        roleService.remove(id);
        return KmallResult.success();
    }

    /**
     * 修改角色
     * @param roleDTO  角色
     * @return 结果
     */
    @PostMapping(value = "/edit")
    public KmallResult edit(@Validated @RequestBody RoleEditDTO roleDTO) {
        Long id = roleDTO.getId();
        SysRole sysRole = roleService.getById(id);
        if (sysRole == null) {
            return KmallResult.fail("角色不存在");
        }
        sysRole.setRoleCode(roleDTO.getRoleCode());
        sysRole.setRoleName(roleDTO.getRoleName());
        sysRole.setStatus(roleDTO.getStatus());
        sysRole.setSort(roleDTO.getSort());
        sysRole.setRemark(roleDTO.getRemark());
        sysRole.setUpdateBy(currentUser().getUsername());
        if (!"admin".equals(sysRole.getRoleCode())) {
            List<SysMenu> menus;
            List<Long> menuIds = roleDTO.getMenuIds();
            if (!CollectionUtils.isEmpty(menuIds)) {
                menus = menuService.listByIds(menuIds);
            } else {
                menus = new ArrayList<>();
            }
            sysRole.setMenus(menus);
        }
        roleService.update(sysRole);
        return KmallResult.success();
    }

    /**
     * 修改角色
     * @param roleDTO  角色
     * @return 结果
     */
    @PostMapping(value = "/edit/status")
    public KmallResult editStatus(@Validated @RequestBody RoleEditDTO roleDTO) {
        Long id = roleDTO.getId();
        SysRole sysRole = roleService.getById(id);
        if (sysRole == null) {
            return KmallResult.fail("角色不存在");
        }
        sysRole.setStatus(roleDTO.getStatus());
        roleService.update(sysRole);
        return KmallResult.success();
    }

    /**
     * 根据角色ID获取详细信息
     * @param id    岗位ID
     * @return 结果
     */
    @GetMapping(value = "/{id}")
    public KmallResult getInfo(@PathVariable Long id) {
        SysRole sysRole = roleService.getById(id);
        if (null == sysRole) {
            throw new ServiceException(KmallResultEnum.DAO_NOT_FOUND);
        }
        return KmallResult.success(toRoleVO(sysRole));
    }

    /**
     * 获取角色列表
     */
    @PostMapping("/list")
    public KmallResult list(@RequestBody RoleQueryDTO queryDTO) {
        Page<SysRole> page = roleService.listByPage(queryDTO);
        return KmallResult.success().put("rows", page.getContent())
                .put("total", page.getTotalElements());
    }

    private RoleVO toRoleVO(SysRole role) {
        RoleVO vo = new RoleVO();
        BeanUtils.copyProperties(role, vo);
        return vo;
    }
}

